local dnet = {}
-- 网络变量
dnet.sock = nil
dnet.buff = zbuff.create(1024)

dnet.host = "120.77.205.203" 
dnet.port = 54321
dnet.name = "ddn_pc"

dnet.isConnect = 0

-- 已经发送数据缓存的表
local cachetable = {}

local function NetEvts(netc, event, param)
    local function doClose()
        -- 将缓存表内容清空，以便下次连接的时候全量发送消息
        cachetable = {}
        dnet.isConnect = 0
        sys.publish("socket_disconnect")
    end

    local function doLink()
    end

    local function doLine()
        dnet.isConnect = 1
    end
    local function doTxOk(netc)
        socket.wait(netc)
    end
    local function doEvent(netc)
        --read data sock --> buff 
        local ret, len = socket.rx(netc, dnet.buff)
        socket.wait(netc)
        --deal data buff --> string
        if dnet.buff:used() > 0 then
            local cmd = dnet.buff:toStr(0, len)
            dnet.buff:del()
            dnet.buff:seek(0)
            local jcmd = json.decode(cmd)
            if jcmd ~= nil then
                sys.publish("ddn_event", {src = "dnet",evt = "COMMAND", dat = jcmd})
            end
        end        
    end
    
    if param ~= 0 then
        doClose()
        return
    end

    local function onLink(netc,event)
        if event == socket.LINK then
            log.info("---------------LINK-------------")
            doLink()
        end
    end
    local function onLine(netc,event)
        if event == socket.ON_LINE then
            log.info("---------------ON_LINE-------------")
            doLine()
        end
    end
    local function onTxOk(netc,event)
        if event == socket.TX_OK then
            log.info("---------------TX_OK-------------")
            doTxOk(netc)
        end
    end
    local function onEvent(netc,event)
        if event == socket.EVENT then
            log.info("---------------EVENT-------------")
            doEvent(netc)
        end
    end
    local function onClose(netc,event)
        if event == socket.CLOSE then
            log.info("---------------CLOSE-------------")
            doClose()
        end
    end

    onLink(netc,event)
    onLine(netc,event)
    onTxOk(netc,event)
    onEvent(netc,event)
    onClose(netc,event)

end

-- 网络初始化任务
local function NetInit()

    mobile.rtime(1)  
    sys.wait(5000)  
    -- 设置SOCKET
    dnet.sock = socket.create(nil, NetEvts)
    log.info("ahah:",dnet.sock)
    socket.debug(dnet.sock, true)
    socket.config(dnet.sock, nil, nil, nil, 300, 5, 6) -- 开启TCP保活，防止长时间无数据交互被运营商断线
    while true do
        local succ, result = socket.connect(dnet.sock, dnet.host, dnet.port)
        log.info("-----socket.connect-----succ---", succ, "-----result-----", result)
        if not succ then
            dnet.isFirstConnect = 0
            log.info("未知错误,5秒后重连")
        else
            local result, msg = sys.waitUntil("socket_disconnect")
        end
        log.info("服务器断开了,5秒后重连")
        socket.close(dnet.sock)
        log.info(rtos.meminfo("sys"))
        sys.wait(5000)
    end
end

-- 网络发送任务
local function NetSend()
    log.info("------开始发送-------")
    local function sendMsg(msg)
        local ret, ret2, ret3 = socket.tx(dnet.sock, msg)
        log.info("发送消息给服务器", msg)
    end
    local function onSend(dat)
        if (dat.src == "dsec" and dat.evt == "HEART") then
            log.info("------dnet 心跳到来------")
            if (dnet.isConnect == 1) then
                sendMsg(string.format("{heart:%05d}\r\n",dat.dat.heart))
            end
        end
    end

    while true do
        local ret, dat = sys.waitUntil("ddn_event", 2000)
        if (ret == true) then
            onSend(dat)
        end
    end
end

--模块初始化
local function Init()
    sys.taskInit(NetInit)
    sys.taskInit(NetSend)
end
dnet.Init = Init
return dnet
